#!/usr/bin/env python3  /*设定脚本文件直接能执行*/
# -*- coding:utf-8 -*-   /*设置编码*/
"""
@Author: zhuo
@Software: PyCharm
@File: tQStateBox.py
@Time: 2024/1/23 9:58
@Function：ctQStateBox：自定义状态框，可以通过传入的参数字典，增加实时状态显示
"""
from PySide6.QtGui import QPixmap
from PySide6.QtWidgets import QWidget, QVBoxLayout, QHBoxLayout, QLabel, QSizePolicy, QSpacerItem, QFrame, QLineEdit

RES_PATH = './ico/'                  # 资源文件基准地址
STATE_ICON = RES_PATH + "state.png"  # 状态图标路径
SLASH_ICON = RES_PATH + "slash.png"  # 斜杠图标路径


class ctQStateBox(QWidget):
    """
    自定义状态框：可实现登录、用户新增删除、用户信息更改功能。
    """

    # 初始化
    def __init__(self, dic):
        """
        function:  初始化
              in:  dic：参数字典
             out:  None
          return:  None
          others:  Initialize
        """
        # 继承自父类
        super(ctQStateBox, self).__init__()
        # frame中的布局：垂直布局
        self.layoutFrame = QVBoxLayout()
        # 参数字典
        self.dicMsg = dic
        self.__f_stateBoxLayoutFunc()

    # 状态框布局函数
    def __f_stateBoxLayoutFunc(self):
        """
        function:  状态框布局函数
              in:  None
             out:  None
          return:  None
          others:  Status Box Layout Func
        """

        # 总布局:垂直布局
        layoutMain = QVBoxLayout()
        self.setLayout(layoutMain)

        # 创建QFrame
        frame = QFrame(self)
        # 设置外线宽度
        frame.setLineWidth(3)
        # 设置中线宽度
        frame.setMidLineWidth(3)
        # 设置边框样式
        frame.setFrameStyle(QFrame.WinPanel | QFrame.Raised)

        # frame中的布局：垂直布局
        self.layoutFrame = QVBoxLayout()
        frame.setLayout(self.layoutFrame)

        # <editor-fold desc="图标行布局">
        # 图标行布局:水平布局
        layoutIcon = QHBoxLayout()
        # 输入为图片路径，比如ico文件内的icon.png图片
        lblIcon = QLabel()
        lblIcon.setPixmap(QPixmap(STATE_ICON))
        lblIcon.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred)
        lblIcon.setFixedSize(20, 20)
        # 设置图标自适应
        lblIcon.setScaledContents(True)
        # 实时状态标签
        lblState = QLabel("实时状态")
        lblState.setStyleSheet("text-decoration: underline;font-weight:bold;")
        lblState.setFixedSize(60, 25)
        # 斜杠标签
        lblSlash = QLabel()
        lblSlash.setPixmap(QPixmap(SLASH_ICON))
        lblSlash.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred)
        lblSlash.setFixedSize(25, 25)
        lblSlash.setScaledContents(True)
        # 创建弹簧
        spacerIcon = QSpacerItem(180, 25, QSizePolicy.Expanding, QSizePolicy.Ignored)
        # 图标行布局添加控件
        layoutIcon.addWidget(lblIcon)
        layoutIcon.addWidget(lblState)
        layoutIcon.addWidget(lblSlash)
        layoutIcon.addItem(spacerIcon)
        # </editor-fold>

        # frame布局中添加图标行
        self.layoutFrame.addLayout(layoutIcon)

        self.__f_addMsgLayout()
        layoutMain.addWidget(frame)

    # 参数信息框布局
    def __f_addMsgLayout(self):
        """
        function:  参数信息框布局
              in:  None
             out:  None
          return:  None
          others:  Parameter Information Box Layout
        """
        for k, v in self.dicMsg.items():
            layout = QHBoxLayout()
            lbl = QLabel(k)
            lbl.setFixedSize(60, 20)
            txt = QLineEdit()
            txt.setObjectName(k)
            txt.setEnabled(False)
            txt.setFixedSize(80, 20)
            if len(v) == 2:
                unit = QLabel(self.dicMsg[k]["unit"])
                unit.setFixedSize(40, 20)
                spacer = QSpacerItem(10, 20, QSizePolicy.Expanding, QSizePolicy.Preferred)
                layout.addWidget(lbl)
                layout.addWidget(txt)
                layout.addWidget(unit)
                layout.addItem(spacer)
            else:
                spacer = QSpacerItem(10, 20, QSizePolicy.Expanding, QSizePolicy.Preferred)
                layout.addWidget(lbl)
                layout.addWidget(txt)
                layout.addItem(spacer)
            self.dicMsg[k]["txt"] = txt
            self.layoutFrame.addLayout(layout)
        # 创建弹簧
        spacerFrame = QSpacerItem(180, 50, QSizePolicy.Expanding, QSizePolicy.Expanding)
        self.layoutFrame.addItem(spacerFrame)
        return self.dicMsg
